package com.ce.pms.system.custservice.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.ISignReadingRuleDao;
import com.ce.pms.system.custservice.model.CustEventKindDO;
import com.ce.pms.system.custservice.model.CustEventNatureDO;
import com.ce.pms.system.custservice.model.EmergencyDegreeDO;
import com.ce.pms.system.custservice.model.SignReadingRuleDO;
import com.ce.pms.system.custservice.service.ISignReadingRuleService;
import com.ce.pms.system.custservice.vo.SignReadingRuleVO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

public class SignReadingRuleServiceImpl extends PmsBaseServiceImpl implements ISignReadingRuleService {
	/**
	 * 方法描述: 分页查询所有阅签规则列表
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param searchVO
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryAllSignReadingRuleByPage(SearchVO searchVO){
		Page page = signReadingRuleDao.queryAllSignReadingRuleByPage(searchVO);
		List<SignReadingRuleDO> readingRuleDOList = page.getResult();
		List<SignReadingRuleVO> readingRuleVOList = new ArrayList<SignReadingRuleVO>();
		for(SignReadingRuleDO readingRuleDO : readingRuleDOList){
			readingRuleVOList.add(this.assembleD2V(readingRuleDO, null));
		}
		page.setResult(readingRuleVOList);
		return page;
	}
	/**
	 * 方法描述: 阅签规则列表查询方法(根据序号升序排序，如果为空则在最后)
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param name
	 * @return List<SignReadingRuleVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<SignReadingRuleVO> getSignReadingRuleList() {
		Page page = signReadingRuleDao.getSignReadingRuleList(null);
		List<SignReadingRuleDO> readingRuleDOList = page.getResult();
		List<SignReadingRuleVO> readingRuleVOList = new ArrayList<SignReadingRuleVO>();
		for(SignReadingRuleDO readingRuleDO : readingRuleDOList){
			readingRuleVOList.add(this.assembleD2V(readingRuleDO, null));
		}
		return readingRuleVOList;
	}
	/**
	 * 方法描述: 根据ID查询VO方法
	 * @author 胡煦
	 * Create on:2008-12-22
	 * @param keyword
	 * @return SignReadingRuleVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public SignReadingRuleVO findSignReadingRuleVOById(Long keyword) {
		if(BlankUtil.isBlank(keyword)){
			return null;
		}
		return this.assembleD2V(signReadingRuleDao.findById(keyword), null);
	}
	/**
	 * 方法描述: 阅签规则增加方法
	 *
	 * @author 胡煦 Create on:2008-12-16
	 * @param SignReadingRuleVO
	 * @return SignReadingRuleVO Edit author: Edit on: 修改原因：
	 */
	public SignReadingRuleVO addSignReadingRule(SignReadingRuleVO readingRuleVO) {
		if(BlankUtil.isBlank(readingRuleVO)){
			return null;
		}
		this.checkSignReadingRuleByName(readingRuleVO.getName(), null);
		SignReadingRuleDO readingRuleDO = this.assembleV2D(readingRuleVO, null);
		readingRuleDO = signReadingRuleDao.saveObject(readingRuleDO);
		readingRuleVO.setKeyword(readingRuleDO.getKeyword());
		return readingRuleVO;
	}
	/**
	 * 方法描述: 阅签规则删除方法
	 *
	 * @author 胡煦 Create on:2008-12-16
	 * @param readingRuleId
	 * @return boolean Edit author: Edit on: 修改原因：
	 */
	public boolean deleteSignReadingRule(Long readingRuleId) {
		if(BlankUtil.isBlank(readingRuleId)){
			return false;
		}
		try {
			signReadingRuleDao.deleteObjectById(readingRuleId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw new BusinessException(
					"custservice:system_delete_inused_exception",
					BusinessException.WARNING);
		}
		return true;
	}
	/**
	 * 方法描述: 阅签规则修改方法
	 *
	 * @author 胡煦 Create on:2008-12-16
	 * @param SignReadingRuleVO
	 * @return SignReadingRuleVO Edit author: Edit on: 修改原因：
	 */
	public SignReadingRuleVO editSignReadingRule(SignReadingRuleVO readingRuleVO) {
		if(BlankUtil.isBlank(readingRuleVO)){
			return null;
		}
		this.checkSignReadingRuleByName(readingRuleVO.getName(), readingRuleVO.getKeyword());
		SignReadingRuleDO readingRuleDO = this.assembleV2D(readingRuleVO, null);
		SignReadingRuleDO oldReadingRuleDO = signReadingRuleDao.findById(readingRuleVO.getKeyword());
		modifyInfo(oldReadingRuleDO.getSysInfo(), readingRuleVO.getModifier());
		BeanUtil.convertVoAsDo(readingRuleDO, oldReadingRuleDO);
		return readingRuleVO;
	}
	/**
	 * 功能描述:校验阅签规则名字是否存在<br>
	 * 注：新建判断name的唯一性时，oldId传为null；修改判断name的唯一性时，则需要传入本身的ID<br>
	 * @param name
	 * @param id
	 * @return
	 * Create author:胡煦
	 * Create on:2008-7-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void checkSignReadingRuleByName(String name,Long id){
		Long count = signReadingRuleDao.countSignReadingRuleByName(name, id);
		if(count > 0){
			throw new BusinessException(
					"custservice:system_name_exist_exception",
					BusinessException.WARNING);
		}
	}

	/**
	 * 方法描述: readingRuleVO To readingRuleDO
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param readingRuleVO
	 * @param readingRuleDO
	 * @return SignReadingRuleDO
	 */
	private SignReadingRuleDO assembleV2D(SignReadingRuleVO readingRuleVO, SignReadingRuleDO readingRuleDO) {
		if(BlankUtil.isBlank(readingRuleDO)){
			readingRuleDO = new SignReadingRuleDO();
		}
		BeanUtil.convertVoAsDo(readingRuleVO, readingRuleDO);
		if(!BlankUtil.isBlank(readingRuleVO.getCustServEventKindId())){
			readingRuleDO.setCustServEventKind(new CustEventKindDO(readingRuleVO.getCustServEventKindId()));

		}
		if(!BlankUtil.isBlank(readingRuleVO.getEmergencyDegreeId())){
			readingRuleDO.setEmergencyDegree(new EmergencyDegreeDO(readingRuleVO.getEmergencyDegreeId()));
		}
		if(!BlankUtil.isBlank(readingRuleVO.getCreator())){
			readingRuleDO.setSysInfo(createInfo(readingRuleVO.getCreator()));
		}
		return readingRuleDO;
	}

	/**
	 * 方法描述: SignReadingRuleDO To SignReadingRuleVO
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param readingRuleDO
	 * @param readingRuleVO
	 * @return SignReadingRuleVO
	 */
	private SignReadingRuleVO assembleD2V(SignReadingRuleDO readingRuleDO, SignReadingRuleVO readingRuleVO) {
		if(BlankUtil.isBlank(readingRuleVO)){
			readingRuleVO = new SignReadingRuleVO();
		}
		PopulateUtil.populate(readingRuleDO, readingRuleVO);
		CustEventNatureDO custEventNatureDO = readingRuleDO.getCustServEventKind()==null?null:readingRuleDO.getCustServEventKind().getCustEventNature();
		readingRuleVO.setCustEventNatureId(custEventNatureDO==null?null:custEventNatureDO.getKeyword());
		readingRuleVO.setCustServEventKindId(readingRuleDO.getCustServEventKind()==null?null:readingRuleDO.getCustServEventKind().getKeyword());
		readingRuleVO.setEmergencyDegreeId(readingRuleDO.getEmergencyDegree()==null?null:readingRuleDO.getEmergencyDegree().getKeyword());
		return readingRuleVO;
	}

	private ISignReadingRuleDao signReadingRuleDao;

	/**
	 * @param signReadingRuleDao the signReadingRuleDao to set
	 */
	public void setSignReadingRuleDao(ISignReadingRuleDao signReadingRuleDao) {
		this.signReadingRuleDao = signReadingRuleDao;
	}

}
